import os
from dotenv import load_dotenv
import qiniu

# Load environment variables from .env file
load_dotenv()

def get_upload_token(expires=3600):
    """
    Generates an upload token for Qiniu Cloud Storage.
    
    :param expires: Token expiration time in seconds.
    :return: A dictionary containing the upload token and the bucket domain.
    """
    # Get Qiniu credentials from environment variables
    access_key = os.getenv("QINIU_ACCESS_KEY")
    secret_key = os.getenv("QINIU_SECRET_KEY")
    bucket_name = os.getenv("QINIU_BUCKET_NAME")
    bucket_domain = os.getenv("QINIU_BUCKET_DOMAIN")

    # Check if all required environment variables are set
    if not all([access_key, secret_key, bucket_name, bucket_domain]):
        raise ValueError("Qiniu credentials are not fully configured in .env file.")

    # Authenticate with Qiniu
    auth = qiniu.Auth(access_key, secret_key)
    token = auth.upload_token(bucket_name, expires=expires)
    return {"token": token, "domain": bucket_domain}

